package com.ss.android.article.base.feature.c;

import android.animation.Animator;
import android.content.Context;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AbsListView;
import android.widget.ListView;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.NetworkType;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.collection.WeakHandler.Callback;
import com.ss.android.account.Spipe;
import com.ss.android.account.b.UserListManager;
import com.ss.android.account.model.PlatformUser;
import com.ss.android.account.model.SpipeUser;
import com.ss.android.action.ArticleActionHelper;
import com.ss.android.action.b.BaseFeedAdapter;
import com.ss.android.action.b.FeedViewHolder;
import com.ss.android.action.b.ImpressionHelper;
import com.ss.android.action.b.ImpressionRecorder;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.e.ArticleShareHelper;
import com.ss.android.article.base.feature.feed.g;
import com.ss.android.article.base.feature.feed.presenter.ArticleFeedPresenter;
import com.ss.android.article.base.feature.model.CellRef;
import com.ss.android.article.base.ui.DiggAnimationView;
import com.ss.android.article.base.utils.JSONObjectWrapper;
import com.ss.android.article.browser.R;
import com.ss.android.common.app.IComponent;
import com.ss.android.common.app.LifeCycleMonitor;
import com.ss.android.common.util.NetworkStatusMonitor;
import com.ss.android.image.loader.LoadImagePolicy;
import com.ss.android.messagebus.SubscriptionRouter;
import com.ss.android.newmedia.BaseAppData;
import com.ss.android.newmedia.a.PopupToast;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

import inline.TempBean;

public final class FeedAdapter extends BaseFeedAdapter implements
        AbsListView.RecyclerListener,
        Callback,
        g,
        LifeCycleMonitor,
        com.ss.android.k.a.b<CellRef> {

    final class b$a implements ViewTreeObserver.OnPreDrawListener {
        private final WeakReference<View> a;
        private final String b;
        private final WeakHandler c;
        private final WeakReference<Context> mContextRef;

        b$a(View view, String arg3, WeakHandler arg4, Context context) {
            this.a = new WeakReference<>(view);
            this.b = arg3;
            this.c = arg4;
            this.mContextRef = new WeakReference<>(context);
        }

        public final boolean onPreDraw() {
            Context context = this.mContextRef.get();
            View view = this.a.get();
            if (context == null || view == null || this.c == null) {
                view.getViewTreeObserver().removeOnPreDrawListener(this);
            } else if ("__all__".equals(this.b)) {
                com.ss.android.l.c.c();
                com.bytedance.ttstat.a.a(context, AppData.getInstance().aD());
                view.getViewTreeObserver().removeOnPreDrawListener(this);
                this.c.post(new Runnable() {
                    @Override
                    public void run() {
                        SubscriptionRouter.c(new FeedAdapter.b$b(true));
                    }
                });
            }

            return true;
        }
    }

    public static final class b$b {
        public final boolean a;

        public b$b(boolean arg1) {
            this.a = arg1;
        }
    }

    final class b$c implements Runnable {
        private final WeakReference a;

        public b$c(FeedAdapter arg2) {
            this.a = new WeakReference(arg2);
        }

        public final void run() {
            Object v0 = this.a.get();
            if (v0 != null) {
                ListView v1 = ((FeedAdapter) v0).getListView();
                if (((FeedAdapter) v0).isPaused) {
                    if (v1 != null && !v1.isShown()) {
                        return;
                    }

                    ((FeedAdapter) v0).f();
                }
            }
        }
    }

    private boolean A = true;
    public List<FeedPresenter> mFeedPresenterList = new ArrayList();
    public final List<CellRef> mCellRefList;
    public AppData mAppData = AppData.getInstance();
    public AtomicBoolean i = new AtomicBoolean(false);
    public boolean isDestroyed = false;
    public String mCategoryId;
    public String mCategoryCity;
    public long mConcernId;
    public final NetworkStatusMonitor mNetworkStatusMonitor;
    public Map<View, Animator> mAnimatorHashMap = new ConcurrentHashMap();
    public int mPreloadCountMax = 0;
    public int mPreloadCountNormal = 0;
    public int mPreloadCountMin = 0;
    private static Set<Class<?>> mFeedPresenterClassSet = new HashSet();
    private FeedItemBehavior FeedItemBehavior;
    private FeedContext mFeedContext;
    private View mHostView;
    private WeakHandler mWeakHandler;
    private IComponent mHostPage;
    private View y;
    private int refer;

    public FeedAdapter(Context context, FeedItemBehavior feedItemBehavior, NetworkStatusMonitor networkStatusMonitor,
                       View hostView, IComponent hostPage, int feedType, DiggAnimationView diggAnimationView,
                       ArticleActionHelper articleActionHelper, ArticleShareHelper articleShareHelper, String categoryId) {
        this(context, feedItemBehavior, networkStatusMonitor, hostView, hostPage, feedType, diggAnimationView, articleActionHelper, articleShareHelper, categoryId, 1);
    }

    public FeedAdapter(Context context, FeedItemBehavior feedItemBehavior, NetworkStatusMonitor networkStatusMonitor,
                       View hostView, IComponent pageHolder, int feedType, DiggAnimationView diggAnimationView,
                       ArticleActionHelper articleActionHelper, ArticleShareHelper articleShareHelper,
                       String categoryId, int refer) {
        super(context);
        Calendar.getInstance();
        Spipe.inst();
        this.mHostView = hostView;
        this.mHostPage = pageHolder;
        this.mWeakHandler = new WeakHandler(this);
        this.FeedItemBehavior = feedItemBehavior;
        this.mCategoryId = categoryId;
        this.mNetworkStatusMonitor = networkStatusMonitor;
        this.mCellRefList = new ArrayList();
        this.mContext.getResources();
        TTUtils.getNightModeColorFilter();
        this.refer = refer;
        this.readPreloadConfig();
        addFeedPresenterClass(ArticleFeedPresenter.class);
        try {
            addFeedPresenterClass(Class.forName("com.ss.android.wenda.c.WendaFeedPresenter"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        FeedContext feedContext = new FeedContext();
        feedContext.mContext = this.mContext;
        feedContext.mFeedItemBehavior = this.FeedItemBehavior;
        feedContext.mCellRefList = this.mCellRefList;
        feedContext.mFeedType = feedType;
        feedContext.mCategoryId = categoryId;
        feedContext.mNetworkMonitor = this.mNetworkStatusMonitor;
        feedContext.mDiggAnimationView = diggAnimationView;
        feedContext.mArticleActionHelper = articleActionHelper;
        feedContext.mArticleShareHelper = articleShareHelper;
        feedContext.mPopupToast = new PopupToast(this.mContext);
        this.mFeedContext = feedContext;
        this.setFeedContext(feedContext);
    }

    private static void addFeedPresenterClass(Class clazz) {
        if (clazz != null) {
            mFeedPresenterClassSet.add(clazz);
        }
    }

    private void setFeedContext(FeedContext feedContext) {
        Iterator<Class<?>> iterator = mFeedPresenterClassSet.iterator();
        while (iterator.hasNext()) {
            Class clazz = iterator.next();
            try {
                FeedPresenter presenter = (FeedPresenter) clazz.newInstance();
                presenter.setFeedContext(feedContext);
                this.mFeedPresenterList.add(presenter);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        }
        if (this.mFeedPresenterList != null && this.mFeedPresenterList.size() != 0) {
            if (android.support.design.a.mFeedPresenterComparator == null) {
                android.support.design.a.mFeedPresenterComparator = new Comparator<FeedPresenter>() {
                    public final int compare(FeedPresenter left, FeedPresenter right) {
                        return left.g() - right.g();
                    }
                };
            }
            Collections.sort(this.mFeedPresenterList, android.support.design.a.mFeedPresenterComparator);
        }
    }

    public final Object a(int index) {
        if (index < 0 || index >= this.mCellRefList.size()) {
            return null;
        } else {
            CellRef cellRef = this.mCellRefList.get(index);
            if (cellRef.cellType == 9 && this.mFeedContext.k.get("extra_taobao_ref") != null) {
                return this.mFeedContext.k.get("extra_taobao_ref");
            } else {
                return cellRef;
            }
        }
    }

    public final void a(long concernId) {
        this.mConcernId = concernId;
        if (this.mFeedPresenterList != null
                && this.mFeedPresenterList.size() != 0
                && this.mFeedContext != null) {
            Iterator<FeedPresenter> feedPresenterIterator = this.mFeedPresenterList.iterator();
            while (feedPresenterIterator.hasNext()) {
                feedPresenterIterator.next();
            }
        }
    }

    public final void setLastReadClickListener(View.OnClickListener onClickListener) {
        for (int i = 0; i < this.mFeedPresenterList.size(); ++i) {
            this.mFeedPresenterList.get(i).setLastReadClickListener(onClickListener);
        }
    }

    public final void a(List<CellRef> list) {
        this.mCellRefList.clear();
        this.mCellRefList.addAll(list);
        for (int i = 0; i < this.mFeedPresenterList.size(); ++i) {
            this.mFeedPresenterList.get(i).f();
        }

        this.notifyDataSetChanged();
    }

    public final void a(boolean arg3) {
        for (int i = 0; i < this.mFeedPresenterList.size(); ++i) {
            this.mFeedPresenterList.get(i).c(arg3);
        }
    }

    public final boolean a() {
        boolean v0 = false;
        if ((this.isPaused) && (this.FeedItemBehavior == null || (this.FeedItemBehavior.isPrimaryPage()))) {
            v0 = true;
        }

        return v0;
    }

    public final boolean a(int arg2, FeedViewHolder arg3) {
        return this.isPaused && this.a();
    }

    public final CellRef b(int position) {
        return position < 0 || position >= this.mCellRefList.size() ? null : this.mCellRefList.get(position);
    }

    @Deprecated
    public final void b(List<CellRef> datas) {
        this.mCellRefList.clear();
        this.mCellRefList.addAll(datas);
        for (int i = 0; i < this.mFeedPresenterList.size(); ++i) {
            this.mFeedPresenterList.get(i).f();
        }
        this.notifyDataSetChanged();
    }

    public final void onPrimaryPageChanged(boolean set) {
        if (set) {
            this.f();
        } else {
            this.g();
        }

        for (int i = 0; i < this.mFeedPresenterList.size(); ++i) {
            this.mFeedPresenterList.get(i).onPrimaryPageChanged(set);
        }
    }

    public final void onHiddenStatusChanged(boolean visible) {
        for (int i = 0; i < this.mFeedPresenterList.size(); ++i) {
            this.mFeedPresenterList.get(i).onHiddenStatusChanged(visible);
        }
        this.notifyDataSetChanged();
    }

    public final com.ss.android.action.b.b d() {
        com.ss.android.action.b.b v0_2;
        String v1;
        if (this.c == null) {
            int v0 = this.refer;
            JSONObjectWrapper v2 = new JSONObjectWrapper();
            v2.put("refer", this.refer);
            if (v0 == 1) {
                if (this.mConcernId > 0) {
                    v2.put("concern_id", String.valueOf(this.mConcernId));
                }

                v1 = !"news_local".equals(this.mCategoryId) || (android.support.design.a.isTextEmpty(this.mCategoryCity))
                        ? this.mCategoryId
                        : this.mCategoryCity;
            } else {
                if (!android.support.design.a.isTextEmpty(this.mCategoryId)) {
                    v2.put("category_id", this.mCategoryId);
                }
                v1 = String.valueOf(this.mConcernId);
            }

            String v2_1 = v2.jsonObject.toString();
            ImpressionHelper v3 = ImpressionHelper.getInstance();
            if (android.support.design.a.isTextEmpty(v1)) {
                v0_2 = null;
            } else {
                if (v3.a == null) {
                    v3.a = new HashMap();
                }

                String v4 = "1_" + v1;
                v0_2 = v3.a.get(v4);
                if (v0_2 != null) {
                } else if (!ImpressionHelper.a(1, v1)) {
                    v0_2 = v3.b();
                } else {
                    v0_2 = new ImpressionRecorder(1, v1, v2_1, v3);
                    v3.a.put(v4, (ImpressionRecorder) v0_2);
                }
            }
            this.c = v0_2;
        }

        return this.c;
    }

    public final void onDestroy() {
        for (int i = 0; i < this.mFeedPresenterList.size(); ++i) {
            this.mFeedPresenterList.get(i).onDestroy();
        }
        this.isDestroyed = true;
        this.j();
    }

    public final void onResume() {
        for (int i = 0; i < this.mFeedPresenterList.size(); ++i) {
            this.mFeedPresenterList.get(i).onResume();
        }

        this.readPreloadConfig();
        this.isPaused = true;
        this.setLoadImagePolicy();
        if (!this.mCellRefList.isEmpty()) {
            this.mHostView.post(new b$c(this));
        }
        this.j();
    }

    public final void onPause() {
        int v1;
        for (v1 = 0; v1 < this.mFeedPresenterList.size(); ++v1) {
            this.mFeedPresenterList.get(v1).onPause();
        }

        this.g();
        this.isPaused = false;
        this.j();
    }

    public final int getCount() {
        return this.mCellRefList.size();
    }

    public final Object getItem(int arg2) {
        return this.mCellRefList.get(arg2);
    }

    public final long getItemId(int arg3) {
        return ((long) arg3);
    }

    public final int getItemViewType(int position) {
        if (!android.support.design.a.isEmptyCollection((Collection<CellRef>) this.mCellRefList)) {
            CellRef cellRef = this.mCellRefList.get(position);
            int itemViewType = 0;
            for (int i = 0; i < this.mFeedPresenterList.size(); i++) {
                itemViewType = this.mFeedPresenterList.get(i).getItemViewType(cellRef);
                if (itemViewType > 0) {
                    return itemViewType;
                }
            }
            return itemViewType;
        } else {
            return 0;
        }
    }

    public final View getView(int position, View convertView, ViewGroup parent) {
        View v1_1;
        View view = null;
        this.setLoadImagePolicy();
        View v0 = null;
        this.getCount();
        for (int i = 0; i < mFeedPresenterList.size(); i++) {
            FeedPresenter presenter = mFeedPresenterList.get(i);
            view = presenter.getView(position, convertView, parent);
            if (view != null) {
                view.setTag(R.id.tag_presenter, presenter);
                v0 = view;
                break;
            }
        }

        if (view == null) {
            if (this.y == null) {
                this.y = new View(this.mContext);
            }
            v1_1 = this.y;
        } else {
            v1_1 = v0;
        }

        if (v1_1 != null) {
            Object tag = v1_1.getTag();
            if (((tag instanceof FeedViewHolder)) && (this.a())) {
                super.e();
                if (this.c != null && tag != null) {
                    this.c.a(((FeedViewHolder) tag));
                    this.b = false;
                }
            }
        }

        if (("__all__".equals(this.mCategoryId)) && (this.A)) {
            this.A = false;
            v1_1.getViewTreeObserver().addOnPreDrawListener(new b$a(v1_1, this.mCategoryId, this.mWeakHandler, this.mContext));
        }

        return v1_1;
    }

    public final int getViewTypeCount() {
        int count = 0;
        for (int i = 0; i < mFeedPresenterList.size(); i++) {
            count += mFeedPresenterList.get(i).getViewTypeCount();
        }
        return count;
    }

    private void readPreloadConfig() {
        this.mPreloadCountMax = this.mAppData.preloadCountMax;
        this.mPreloadCountNormal = this.mAppData.preloadCountNormal;
        this.mPreloadCountMin = this.mAppData.preloadCountMin;
    }

    public final void handleMsg(Message message) {
        Object v0;
        long v4 = 0;
        int v2 = 1005;
        if (this.mHostPage != null && (this.mHostPage.isViewValid())) {
            int v1 = message.what;
            if ((message.obj instanceof SpipeUser)) {
                v0 = message.obj;
                if (v1 == v2) {
                    BaseAppData v1_1 = BaseAppData.inst();
                    Context v2_1 = this.mContext;
                    if (v1_1.cm == null) {
                        v1_1.cm = new UserListManager(v2_1);
                    }

                    UserListManager v2_2 = v1_1.cm;
                    if (((SpipeUser) v0).isFollowing()) {
                        v2_2.a(((SpipeUser) v0));
                    } else {
                        v2_2.b(((SpipeUser) v0));
                    }

                    Iterator v0_1 = v1_1.co.iterator();
                    while (v0_1.hasNext()) {
                        v0_1.next();
                    }
                } else {
                    if (message.arg1 == 105) {
                        Spipe.inst().a(true);
                    }

                    if (this.mHostView == null) {
                    } else {
                        this.mFeedContext.mPopupToast.showToast(this.mContext.getString(com.ss.android.article.browser.R.string.social_toast_fail_action));
                    }
                }
                this.notifyDataSetChanged();
            } else {
                if ((message.obj instanceof PlatformUser)) {
                    if (v1 != v2 && this.mHostView != null) {
                        this.mFeedContext.mPopupToast.showToast(this.mContext.getString(com.ss.android.article.browser.R.string.social_toast_fail_invite));
                    }

                    this.notifyDataSetChanged();
                    return;
                }

                if (!(message.obj instanceof CellRef)) {
                    return;
                }

                v0 = message.obj;
                if (!((CellRef) v0).e()) {
                    return;
                }

                if (v1 != 10) {
                    return;
                }

                if (((CellRef) v0).af == null) {
                    return;
                }

                if (!TempBean.b()) {
                    return;
                }

                ListView v2_3 = this.getListView();
                if (v2_3 == null) {
                    return;
                }

                for (v1 = 0; v1 < v2_3.getChildCount(); ++v1) {
                    View v0_2 = v2_3.getChildAt(v1);
                    if (v0_2 != null) {
                        v0 = v0_2.getTag();
                        if ((v0 instanceof com.ss.android.article.base.feature.feed.h)) {
                            CellRef v0_3 = ((com.ss.android.article.base.feature.feed.h) v0).a();
                            if (v0_3 != null && (v0_3.e()) && v0_3.af != null && (TempBean.b())) {
                                if (v4 != v4) {
                                    continue;
                                }
                                return;
                            }
                        }
                    }
                }
            }
        }
    }

    private void setLoadImagePolicy() {
        if (this.mNetworkStatusMonitor != null) {
            NetworkType networkType = this.mNetworkStatusMonitor.mCurrrentNetworkType;
            boolean isLoadImage4G = this.mAppData.getAbSettings().isLoadImage4G() && networkType == NetworkType.MOBILE_4G;
            LoadImagePolicy loadImagePolicy = LoadImagePolicy.ALWAYS;
            if (networkType != NetworkType.NONE && networkType != NetworkType.WIFI && mAppData.mLoadImagePref == 2 && !isLoadImage4G) {
                loadImagePolicy = LoadImagePolicy.NEVER;
            }
            for (int i = 0; i < mFeedPresenterList.size(); i++) {
                this.mFeedPresenterList.get(i).setLoadImagePolicy(loadImagePolicy);
            }
        }
    }

    private void j() {
        ListView listView = this.getListView();
        if (listView != null) {
            for (int i = 0; i < listView.getCount(); ++i) {
                View view = listView.getChildAt(i);
                if (view != null) {
                    view.getTag();
                }
            }
        }
    }

    public final void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    public final void onStop() {
        for (int i = 0; i < this.mFeedPresenterList.size(); ++i) {
            this.mFeedPresenterList.get(i).onStop();
        }
    }

    @Override
    public final void onMovedToScrapHeap(View view) {
        super.onMovedToScrapHeap(view);
        Animator animator = this.mAnimatorHashMap.get(view);
        if (animator != null) {
            animator.cancel();
        }

        FeedPresenter feedPresenter = (FeedPresenter) view.getTag(com.ss.android.article.browser.R.id.tag_presenter);
        if (feedPresenter != null) {
            feedPresenter.onMovedToScrapHeap(view);
        }
    }


}

